<?php
namespace Home\Controller;
use Think\Controller;

class CarController extends HomeController {
	public function index(){
		$map =array('username' =>get_username(),'status'=>'1');
		$car = M('carshop')->where($map)->select();
		// var_dump($car,M()->getLastSql());
        // var_dump($car);
		$pic = M()->field('a.gid,b.pic,b.name,b.des')->table('s_carshop as a,s_goodshop
			as b')->where('a.gid = b.id')->select();
		// var_dump($car);
		foreach ($car as $k => $v) {
			$car[$k]['pic'] = $pic[$k]['pic'];
			$car[$k]['name'] = $pic[$k]['name'];
			$car[$k]['des'] = $pic[$k]['des'];
			$sum += $car[$k]['num']*$car[$k]['mprice'];
			
		}
        // var_dump($car,M()->getLastSql());
		$this->assign('sum',$sum);
		$this->assign('car',$car);
		$this->display();
	}

public function add(){

        $time = time();
        $map['gid'] = I('post.gid');
        $map['attrvid']=I('post.attrvid');
        $res = M('attrprice')->where($map)->find();
        $maxnum = $res['num'];
        $tt['num'] = I('post.num');
                if(!is_numeric($tt['num'])){
                    $tt['num'] = 1;
                }
                if($tt['num']==0){
                    $tt['num'] = 1;
                }

                if($tt['num']>$maxnum){
                    $tt['num'] = $maxnum;
                }
        if ($res&&$maxnum!=0) {
        	$map['username'] = get_username();
            $map['status'] = 1;
            $ress = M('carshop')->where($map)->find();
            // var_dump($ress,M()->getLastSql());
            if($ress){
                $where = array('id'=>$ress['id']);
                $shop['ctime'] = $time;
                $shop['num'] = $ress['num'] + $tt['num'];
                $ee = M('carshop')->where($where)->save($shop);
                if($ee){
                    $dat['info'] = "您又添加了一件同款到购物车哦！";
                    $dat['msg'] = "success";
                    $dat['status'] = 1;
                }
            }else{
                $map['ctime'] = $time;
                $map['mprice'] = $res['mprice'];
                $map['num'] = $tt['num'];
                $ress = M('carshop')->add($map);
                if ($ress) {
                    $dat['info'] = "您已成功添加到购物车！";
                    $dat['msg'] = "success";
                    $dat['status'] = 1;
                }else{
                    $dat['info'] = "您可能没登录！";
                    $dat['msg'] = "failed";
                    $dat['status'] = 0;
                }
            }
        }else{
            $dat['info'] = "您选商品属性了么？店小二找不到这个商品啊！";
            $dat['msg'] = "failed";
            $dat['status'] = 0;
        }
        $this->ajaxReturn($dat);
    }


	public function update(){
		$data['num']=I('post.x');
		$id=I('post.id');
		$car=M('carshop');
		$res=$car->where(array('id'=>$id))->save($data);
	}

	public function delete(){
		$data['status']=0;
		$id=I('get.id');
		$car=M('carshop');
		$res=$car->where(array('id'=>$id))->save($data);
		if ($res) {
			$this->success('删除成功');
		}else{
			$this->error(M()->getLastSql());
		}
	}

	public function submitpath(){
     
        $id = array_unique((array)I('id',0));
       
        $id = is_array($id) ? implode(',',$id) : $id;

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        $path = M('carshop')->where($map)->select();
        // $pic = M()->field('a.gid,b.pic,b.name,b.des')->table('s_carshop as a,s_goodshop
        //     as b')->where($map)->where("a.gid = b.id")->select();
        foreach ($path as $k => $v) {
            
            $res =  M('goodshop')->field('pic')->where("id =". $v['gid'])->find();
            $path[$k]['pic']=$res['pic'];
            $sum += $v['num']*$v['mprice'];
            
        }
             // var_dump($path);
        // var_dump(M()->getLastSql());
        session('sum',$sum);
        session('map',$map);
        // var_dump(session('map'));
        $this->assign('sum',$sum);
        $this->assign('path',$path);
        $this->display();
    }
	

	public function truesubmit(){
        $map =session('map');
        if (IS_POST) {
            $hao =time().rand(100,1000);
            $data =array('status'=>2,'ctime'=>time(),'hao'=>$hao);
            $res = M('carshop')->where($map)->save($data);
            // var_dump($res,$data,M('carshop')->getLastSql());die;
            if ($res) {
               session('status',2); 
               $this->success('购买成功');
            }else{
               $this->success('你以经购买');
            }
        }else{
            $this->display();
        }
		
	}

}